package com.commerceerp.system.service.Impl;

import com.commerceerp.system.entity.FxOrderInfo;
import com.commerceerp.system.entity.FxOrderRecord;
import com.commerceerp.system.entity.FxUserInfo;
import com.commerceerp.system.mapper.*;
import com.commerceerp.system.service.FxOrderInfoService;
import com.commerceerp.system.service.FxUserService;
import com.commerceerp.system.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Service
@Transactional
public class FxOrderInfoServiceImpl implements FxOrderInfoService {

    @Autowired
    private FxOrderInfoMapper fxOrderInfoMapper;

    @Autowired
    private FxGoodInfoMapper fxGoodInfoMapper;

    @Autowired
    private FxOrderRecordMapper fxOrderRecordMapper;

    @Autowired
    private FxUserInfoMapper fxUserInfoMapper;

    @Autowired
    private FxRoleInfoMapper fxRoleInfoMapper;

    @Override
    public Integer addNewFxOrderInfo(FxOrderInfo fxOrderInfo) {

        String fxOrderOutUserId = this.fxGoodInfoMapper.getFxOutGoodUserId(fxOrderInfo.getFxOrderGoodId().toString());
        fxOrderInfo.setFxOrderOutUserId(fxOrderOutUserId);

        Integer success = this.fxOrderInfoMapper.addNewFxOrderInfo(fxOrderInfo);

        return success;
    }


    /**
     * 查询分销订单信息
     * @param beginTime
     * @param endTime
     * @param userName
     * @param goodName
     * @return
     */
    @Override
    public List<Map> getFxOrderInfoByState(String inUserId,String beginTime, String endTime, String userName, String goodName,String fxOrderState) {

        List<Map> list = this.fxOrderInfoMapper.getFxOrderInfoByState(inUserId,beginTime,  endTime,  userName,  goodName, fxOrderState);

        for(Map<String,Object> fxOrderInfo : list){

            String orderSkuNumber = fxOrderInfo.get("fxOrderSkuNumber").toString();

            String[] orderSkuNumbers = orderSkuNumber.split(",");
            List<String> SkuNumbers = Arrays.asList(orderSkuNumbers);

            List<Map> skuAndNumbers = new ArrayList<Map>();
            for(String SkuNumber : SkuNumbers){
                Integer index = SkuNumber.indexOf("_");
                String sku = SkuNumber.substring(0,index);
                String Number = SkuNumber.substring(index+1,SkuNumber.length());

                Map<String,Object> snMap = new HashMap<String,Object>();
                snMap.put("sku",sku);
                snMap.put("Number",Number);
                skuAndNumbers.add(snMap);
            }
            fxOrderInfo.put("skuAndNumber",skuAndNumbers);

        }
        return list;
    }


    /**
     * 更改分销商品信息
     * @param fxOrderInfo
     * @return
     */
    @Override
    public Integer updateFxOrderInfo(String type,String fxOrderCancelReason,FxOrderInfo fxOrderInfo) {

        if("1".equals(type)){

            //支付操作
            fxOrderInfo.setFxOrderState("2");
            fxOrderInfo.setFxOrderPayTime(DateUtil.getYMDHMSDate());

            Map<String,Object> fxOrderMap = this.fxOrderInfoMapper.getFxOrderInfoByFxOrderId(fxOrderInfo.getFxOrderId());

            String fxOrderPrice = fxOrderMap.get("goodPrice").toString();
            String fxorder_sku_number = fxOrderMap.get("fxOrderSkuNumber").toString();

            String[] orderSkuNumbers = fxorder_sku_number.split(",");
            List<String> SkuNumbers = Arrays.asList(orderSkuNumbers);

            //订单总价
            Integer allNum = 0;
            for(String SkuNumber : SkuNumbers){
                Integer index = SkuNumber.indexOf("_");
                String Number = SkuNumber.substring(index+1,SkuNumber.length());
                allNum = allNum + Integer.parseInt(Number);
            }

            BigDecimal allOrderMoney = new BigDecimal(allNum.toString()).multiply(new BigDecimal(fxOrderPrice));

            //佣金金额
            BigDecimal forCommisionMoney = new BigDecimal(allOrderMoney.toString()).multiply(new BigDecimal(fxOrderMap.get("commision").toString()));

            //实得金额
            BigDecimal realMoney = allOrderMoney.subtract(forCommisionMoney);

            //添加个人账户分销记录
            FxOrderRecord fxOrderRecord = new FxOrderRecord();
            fxOrderRecord.setForOrderId(fxOrderInfo.getFxOrderId());
            fxOrderRecord.setForCreateTime(DateUtil.getYMDHMSDate());
            fxOrderRecord.setForOrderPrice(allOrderMoney.toString());
            fxOrderRecord.setForRealPrice(realMoney.toString());
            fxOrderRecord.setForRemark("分销收入");
            fxOrderRecord.setForState("1");
            fxOrderRecord.setForCommisionMoney(forCommisionMoney.toString());

            Integer addRecord = this.fxOrderRecordMapper.addNewFxOrderRecord(fxOrderRecord);

            //增加用户账号余额
            FxUserInfo fxUserInfo = this.fxUserInfoMapper.getFxUserInfoByUserId(fxOrderMap.get("loginUserId").toString());

            BigDecimal accountMoney = new BigDecimal(fxUserInfo.getAccountMoney() == null ? "0" : fxUserInfo.getAccountMoney());
            BigDecimal newAccountMoney = accountMoney.add(realMoney);

            FxUserInfo fxUser = new FxUserInfo();
            fxUser.setAccountMoney(newAccountMoney.toString());
            fxUser.setUserId(Integer.parseInt(fxOrderMap.get("loginUserId").toString()));
            Integer updUserInfo = this.fxUserInfoMapper.updateFxUserInfo(fxUser);


        }else if("2".equals(type)){
            fxOrderInfo.setFxOrderState("3");
        }else if("3".equals(type)){
            fxOrderInfo.setFxOrderState("4");
            fxOrderInfo.setFxOrderCancelTime(DateUtil.getYMDHMSDate());
            fxOrderInfo.setFxOrderCancelReason(fxOrderCancelReason);
        }

        Integer success = this.fxOrderInfoMapper.updateFxOrderInfo(fxOrderInfo);

        return 1;

    }


    /**
     * 个人分销记录列表
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public List<Map> getFxOrderRecord(String beginTime, String endTime,String fxOrderRecordType,String fxOrderRecorderStatus) {

        List<Map> list = this.fxOrderRecordMapper.getFxOrderRecord(beginTime,endTime,fxOrderRecordType,fxOrderRecorderStatus);

        return list;
    }


    /**
     * 账户提现
     * @param fxOrderRecord
     * @return
     */
    @Override
    public Integer updFxOrderMoneyTX(FxOrderRecord fxOrderRecord,String userId) {

        Integer success = this.fxOrderRecordMapper.addNewFxOrderRecord(fxOrderRecord);

        //减少用户账号余额
        FxUserInfo fxUserInfo = this.fxUserInfoMapper.getFxUserInfoByUserId(userId);

        BigDecimal accountMoney = new BigDecimal(fxUserInfo.getAccountMoney() == null ? "0" : fxUserInfo.getAccountMoney());
        BigDecimal newAccountMoney = accountMoney.subtract(new BigDecimal(fxOrderRecord.getForOrderOutMoney()));

        FxUserInfo fxUser = new FxUserInfo();
        fxUser.setAccountMoney(newAccountMoney.toString());
        fxUser.setUserId(Integer.parseInt(userId));
        Integer updUserInfo = this.fxUserInfoMapper.updateFxUserInfo(fxUser);

        return success;
    }




}
